layui.config({
    base: "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index', 'form', 'jquery', 'upload', 'table', 'laydate', 'util'], function () {
    var form = layui.form,
        upload = layui.upload,
        laydate = layui.laydate,
        util = layui.util,
        admin = layui.admin;

    form.verify({
        logoVerify: function (value) {
            if (value == null || value == undefined || value == '') {
                return 'logo上传成功才能提交!!!!';
            }
        },
        protocolVerify: function (value) {
            if (value == null || value == undefined || value == '') {
                return '授权函上传成功才能提交!!!!';
            }
        }
    });
    /**
     * 暂存
     */
    form.on('submit(cpn_form_ok)', function (data) {

        //一级类目
        var primaryId = $('#primary').val();
        var primary_name = $('#primary').find("option:selected").text();
        data.field.primary_category_id = primaryId;
        data.field.primary_name = primary_name;

        //二级类目
        var secondaryId = $('#secondary').val();
        var secondary_name = $('#secondary').find("option:selected").text();
        data.field.secondary_category_id = secondaryId;
        data.field.secondary_name = secondary_name;

        layer.confirm("确认暂存吗，切记不要重复点击", {
            btn: ["确认", "取消"]
        }, function (index, layero) {
            $.ajax({
                url: "/cpnmachinfo/update",
                data: data.field,
                type: "post",
                success: function (result) {
                    if (result.code == 1) {
                        layer.msg("编辑成功", {
                            offset: '32%'
                            , icon: 1
                            , time: 0
                            , btn: '知道了'
                            , btnAlign: "c"
                            , shade: 0.4
                            , yes: function () {
                                layer.closeAll();
                                location.reload();
                            }
                        })
                    } else {
                        layer.msg(result.message, {
                            offset: '32%'
                            , icon: 2
                            , time: 3000
                        }, function () {
                            layer.closeAll();
                        })
                    }
                }
            });
        }, function (index) {

        });

        return false;
    });


    /**
     * 提交进件
     */
    form.on('submit(cpn_form_apply)', function (data) {


        //一级类目
        var primaryId = $('#primary').val();
        var primary_name = $('#primary').find("option:selected").text();
        data.field.primary_category_id = primaryId;
        data.field.primary_name = primary_name;

        //二级类目
        var secondaryId = $('#secondary').val();
        var secondary_name = $('#secondary').find("option:selected").text();
        data.field.secondary_category_id = secondaryId;
        data.field.secondary_name = secondary_name;

        layer.confirm("确认提交卡券进件吗，切记不要重复点击", {
            btn: ["确认", "取消"]
        }, function (index, layero) {
            $.ajax({
                url: "/cpnmachinfo/submit",
                data: data.field,
                type: "post",
                success: function (result) {
                    if (result.code == 1) {
                        layer.msg("提交成功", {
                            offset: '32%'
                            , icon: 1
                            , time: 0
                            , btn: '知道了'
                            , btnAlign: "c"
                            , shade: 0.4
                            , yes: function () {
                                layer.closeAll();
                                location.reload();
                            }
                        })
                    } else {
                        layer.msg(result.message, {
                            offset: '32%'
                            , icon: 2
                            , time: 3000
                        }, function () {
                            layer.closeAll();
                        })
                    }
                }
            });
        }, function (index) {

        });

        return false;
    });


    /**
     * 修改后重新提交进件
     */
    form.on('submit(cpn_form_modify_submit)', function (data) {


        //一级类目
        var primaryId = $('#primary').val();
        var primary_name = $('#primary').find("option:selected").text();
        data.field.primary_category_id = primaryId;
        data.field.primary_name = primary_name;

        //二级类目
        var secondaryId = $('#secondary').val();
        var secondary_name = $('#secondary').find("option:selected").text();
        data.field.secondary_category_id = secondaryId;
        data.field.secondary_name = secondary_name;

        layer.confirm("确认修改后重新提交卡券进件吗，切记不要重复点击", {
            btn: ["确认", "取消"]
        }, function (index, layero) {
            $.ajax({
                url: "/cpnmachinfo/modifySubmit",
                data: data.field,
                type: "post",
                success: function (result) {
                    if (result.code == 1) {
                        layer.msg("提交成功", {
                            offset: '32%'
                            , icon: 1
                            , time: 0
                            , btn: '知道了'
                            , btnAlign: "c"
                            , shade: 0.4
                            , yes: function () {
                                layer.closeAll();
                                location.reload();
                            }
                        })
                    } else {
                        layer.msg(result.message, {
                            offset: '32%'
                            , icon: 2
                            , time: 3000
                        }, function () {
                            layer.closeAll();
                        })
                    }
                }
            });
        }, function (index) {

        });

        return false;
    });


    //logo上传和回显
    var logo_img_value = $(".logo_file_input").val();
    if (logo_img_value != null && logo_img_value != "" && logo_img_value != undefined) {
        $(".logo_file_img").attr('src', logo_img_value);
    }
    var logo_upload_btn = $(".logo_file_btn");
    var uploadInst = upload.render({
        elem: logo_upload_btn
        , url: '/cpnmachinfo/logoUpload'
        , data: {}
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $(".logo_file_img").attr('src', result); //图片链接（base64）
            });
        }
        , done: function (res) {
            if (res.code == 1) {
                //如果上传成功
                $(".logo_file_input").val(res.data.logo_url);
                return layer.msg('上传成功');
            } else {
                //上传失败
                $(".logo_file_img").attr('src', ""); //图片链接（base64）
                return layer.msg(res.message);
            }
        }
        , error: function () {
            //演示失败状态，并实现重传
            fade_file_tip.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            fade_file_tip.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }
    });


    $(".face_file").each(function () {


        var file_btn = $(this).find(".face_file_btn");

        var media_file_img = $(this).find(".media_file_img");

        var media_file_input = $(this).find(".media_file_input");

        var keyName = media_file_input.attr("keyName");

        var img_value = media_file_input.val();

        if (img_value != null && img_value != "" && img_value != undefined) {
            var img_url = "/cpnmachinfo/getMaterial?mediaId=" + img_value;
            media_file_img.attr('src', img_url); //图片链接（base64
        }

        var uploadInst = upload.render({
            elem: file_btn
            , url: '/cpnmachinfo/materialUpload'
            , data: {keyName: keyName}
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    media_file_img.attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                if (res.code == 1) {
                    //如果上传成功
                    media_file_input.val(res.data.media_id);
                    return layer.msg('上传成功');
                } else {
                    //上传失败
                    media_file_img.attr('src', '');
                    return layer.msg(res.message);
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                fade_file_tip.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                fade_file_tip.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
    })

    //日期时间选择器
    laydate.render({
        elem: '#end_date'
        , format: 'yyyy年MM月dd日'
    });

    //获取类目列表
    var categoryData = [];
    $.ajax({
        type: "GET",
        url: "/cpnmachinfo/getapplyprotocol",
        async: false,
        success: function (res) {
            categoryData = res.category;
        }
    });
    //获得一级类目下拉框对象

    var primary = $("#primary");
    //获得二级类目下拉框对象
    var secondary = $("#secondary");
    var p_id = $('#primary_category_id').val();
    var s_id = $('#secondary_category_id').val();
    //遍历一级类目数据,并把里面省的数据追加到option选项中
    layui.each(categoryData, function (index) {
        var s_data = categoryData[index].secondary_category;
        if (p_id == categoryData[index].primary_category_id) {
            primary.append('<option class="primaryOption" selected value=' +
                categoryData[index].primary_category_id + '>' +
                categoryData[index].category_name + '</option>');

            // 二级回显
            layui.each(s_data, function (ind) {
                if (s_id == s_data[ind].secondary_category_id) {
                    secondary.append('<option class="secondaryOption" selected value=' +
                        s_data[ind].secondary_category_id + '>' +
                        s_data[ind].category_name + '</option>');
                } else {
                    secondary.append('<option class="secondaryOption" value=' +
                        s_data[ind].secondary_category_id + '>' +
                        s_data[ind].category_name + '</option>');
                }

            });
        } else if (p_id == '') {
            primary.append('<option class="primaryOption" value=' +
                categoryData[index].primary_category_id + '>' +
                categoryData[index].category_name + '</option>');
            layui.each(s_data, function (ind) {
                secondary.append('<option class="secondaryOption" value=' +
                    s_data[ind].secondary_category_id + '>' +
                    s_data[ind].category_name + '</option>');

            });

        } else {
            primary.append('<option class="primaryOption" value=' +
                categoryData[index].primary_category_id + '>' +
                categoryData[index].category_name + '</option>');

        }
    });
    form.render('select');


    // 一级类目下拉框发生改变事件;
    form.on('select(primary)', function (data) {

        //获取当前点击对象的值
        var primaryId = data.value;
        var primary_name = data.elem[data.elem.selectedIndex].text;
        $('#primary_category_id').val(primaryId);
        $('#primary_name').val(primary_name);
        var secondaryData;
        for (var i = 0; i < categoryData.length; i++) {
            if (primaryId == categoryData[i].primary_category_id) {
                secondaryData = categoryData[i].secondary_category;
            }

        }
        secondary.empty();
        //遍历获得二级数据
        layui.each(secondaryData, function (index) {
            secondary.append('<option class="secondaryOption" value=' +
                secondaryData[index].secondary_category_id + '>' +
                secondaryData[index].category_name + '</option>');
        });
        form.render('select');
    });

    form.on('select(secondary)', function (data) {
        //获取当前点击对象的值
        // var secondaryId = data.value;
        // var secondary_name=data.elem[data.elem.selectedIndex].text;
        // $('#secondary_category_id').val(secondaryId);
        // $('#secondary_name').val(secondary_name);
    });


});



